package orest.back.market;

import java.sql.Connection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import orest.back.userneeds.UserNeedsSql;

import net.ssta.common.Constants;
import net.ssta.common.LoginUser;
import net.ssta.core.base.BaseException;
import net.ssta.core.base.ErrorCode;
import net.ssta.core.base.HandlerBase;
import net.ssta.core.db.DBConnectionProvider;
import net.ssta.core.db.DataBaseExecutor;
import net.ssta.core.db.PagingInfo;
import net.ssta.core.db.Record;
import net.ssta.core.db.RecordSet;
import net.ssta.core.web.RequestKit;
import net.ssta.core.util.DateUtility;


/**
 * 
 * @ClassName: MarketHandler
 * @Description: 需求市场相关处理
 * @author: zhanggk
 * @date: Feb 29, 2012
 * 
 */
public class MarketHandler extends HandlerBase
{
    
    /**
     * 获取用户提交的技术需求信息
     */
    public RecordSet getTechneedList(RequestKit requestKit, PagingInfo pagingInfo)
    {
        Connection conn = null;
        RecordSet recordSet = null;
        try
        {
            conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
            DataBaseExecutor m_executor = DataBaseExecutor.getExecutor(conn);
            String sql = MarketSql.getTechneedListSql(requestKit);
            
            recordSet = m_executor.find(sql, pagingInfo);
            return recordSet;
        }
        catch (SQLException e)
        {
            throw new BaseException(ErrorCode.DB_EXCEPTION, e);
        }
        finally
        {
            if (conn != null)
            {
                try
                {
                    conn.close();
                }
                catch (SQLException e)
                {
                    throw new BaseException(ErrorCode.CON_NOT_CLOSE, e);
                }
            }
        }
    }
    
    /**
     * 根据uuid获取技术需求的详细信息
     */
    public Record getTechneedById(String uuid)
    {
        Connection conn = null;
        RecordSet recordSet = null;
        Record record = null;
        try
        {
            conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
            DataBaseExecutor m_executor = DataBaseExecutor.getExecutor(conn);
            String sql = MarketSql.getTechneedInfoSql(uuid);
            recordSet = m_executor.find(sql);
            if(recordSet!=null && recordSet.size()>0){
            	record = recordSet.get(0);
            }
            
            return record;
        }
        catch (SQLException e)
        {
            throw new BaseException(ErrorCode.DB_EXCEPTION, e);
        }
        finally
        {
            if (conn != null)
            {
                try
                {
                    conn.close();
                }
                catch (SQLException e)
                {
                    throw new BaseException(ErrorCode.CON_NOT_CLOSE, e);
                }
            }
        }
    }
    
    /**
     * 修改技术需求信息的审批状态，将指定的技术需求信息的审批状态设置为指定的状态
     * @param techneedid:技术需求的uuid
     * @param ifcheck:审核状态，1审核通过，2审核不通过
     */
    public void techneedCheckStatus(String techneedid, String ifcheck)
    {
        Connection conn = null;
        try
        {
            conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
        	
        	//将指定的技术需求信息审批状态设置为审批通过状态
        	DataBaseExecutor m_executor = DataBaseExecutor.getExecutor(conn);
        	String sql = MarketSql.updateTechneedCheckSql(techneedid, ifcheck);
        	m_executor.execute(sql);
        }
        catch (SQLException e)
        {
            throw new BaseException(ErrorCode.DB_EXCEPTION, e);
        }
        finally
        {
            if (conn != null)
            {
                try
                {
                    conn.close();
                }
                catch (SQLException e)
                {
                    throw new BaseException(ErrorCode.CON_NOT_CLOSE, e);
                }
            }
        }
    }
    
    /**
     * 修改技术需求信息的推荐状态，将指定的技术需求信息的推荐状态设置为指定的状态，同时设置首页推荐和栏目推荐两个标识
     * @param techneedid:技术需求的uuid
     * @param recommend:推荐装填，0：不推荐，1：推荐
     */
    public void techneedRecommend(String techneedid, String recommend)
    {
        Connection conn = null;
        try
        {
            conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
        	//将指定的技术需求信息审批状态设置为审批通过状态
        	DataBaseExecutor m_executor = DataBaseExecutor.getExecutor(conn);
        	String sql = MarketSql.updateTechneedRecommendSql(techneedid, recommend);
        	m_executor.execute(sql);
        }
        catch (SQLException e)
        {
            throw new BaseException(ErrorCode.DB_EXCEPTION, e);
        }
        finally
        {
            if (conn != null)
            {
                try
                {
                    conn.close();
                }
                catch (SQLException e)
                {
                    throw new BaseException(ErrorCode.CON_NOT_CLOSE, e);
                }
            }
        }
    }
    
    /**
     * 删除技术需求信息
     * for ajax
     * @param techneedid:技术需求的uuid
     */
    public void deleteTechneed(RequestKit requestKit)
    {
    	Connection conn = null;
        try
        {
            conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
        	
        	String uuids = requestKit.getParameter("uuids", "");
        	
        	//删除用户项目需求
        	DataBaseExecutor m_executor = DataBaseExecutor.getExecutor(conn);
        	String uuid[] = uuids.split(",");
        	if(uuid!=null&&uuid.length>0){
        		for(int i=0;i<uuid.length;i++){
        			//逐条删除项目需求
        			String sql = MarketSql.deleteTechneedSql(uuid[i]);
        			m_executor.execute(sql);
        		}
        	}
        }
        catch (SQLException e)
        {
            throw new BaseException(ErrorCode.DB_EXCEPTION, e);
        }
        finally
        {
            if (conn != null)
            {
                try
                {
                    conn.close();
                }
                catch (SQLException e)
                {
                    throw new BaseException(ErrorCode.CON_NOT_CLOSE, e);
                }
            }
        }
    }
    /**
     * 获取用户提交的自荐项目信息
     */
    public RecordSet getSelloneselfList(RequestKit requestKit, PagingInfo pagingInfo)
    {
        Connection conn = null;
        RecordSet recordSet = null;
        try
        {
            conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
            DataBaseExecutor m_executor = DataBaseExecutor.getExecutor(conn);
            String sql = MarketSql.getSelloneselfListSql(requestKit);
            recordSet = m_executor.find(sql, pagingInfo);
            return recordSet;
        }
        catch (SQLException e)
        {
            throw new BaseException(ErrorCode.DB_EXCEPTION, e);
        }
        finally
        {
            if (conn != null)
            {
                try
                {
                    conn.close();
                }
                catch (SQLException e)
                {
                    throw new BaseException(ErrorCode.CON_NOT_CLOSE, e);
                }
            }
        }
    }
    /**
     * 根据uuid获取自荐项目的详细信息
     */
    public Record getSelloneselfById(String uuid)
    {
        Connection conn = null;
        RecordSet recordSet = null;
        Record record = null;
        try
        {
            conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
            DataBaseExecutor m_executor = DataBaseExecutor.getExecutor(conn);
            String sql = MarketSql.getSelloneselfInfoSql(uuid);
            recordSet = m_executor.find(sql);
            if(recordSet!=null && recordSet.size()>0){
            	record = recordSet.get(0);
            }
            
            return record;
        }
        catch (SQLException e)
        {
            throw new BaseException(ErrorCode.DB_EXCEPTION, e);
        }
        finally
        {
            if (conn != null)
            {
                try
                {
                    conn.close();
                }
                catch (SQLException e)
                {
                    throw new BaseException(ErrorCode.CON_NOT_CLOSE, e);
                }
            }
        }
    }
    /**
     * 修改自荐项目信息的审批状态，将指定的自荐项目信息的审批状态设置为指定的状态
     * @param selloneselfid:自荐项目的uuid
     * @param ifcheck:审核状态，1审核通过，2审核不通过
     */
    public void selloneselfCheckStatus(String selloneselfid, String ifcheck)
    {
        Connection conn = null;
        try
        {
            conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
        	
        	//将指定的技术需求信息审批状态设置为审批通过状态
        	DataBaseExecutor m_executor = DataBaseExecutor.getExecutor(conn);
        	String sql = MarketSql.updateSelloneselfCheckSql(selloneselfid, ifcheck);
        	m_executor.execute(sql);
        }
        catch (SQLException e)
        {
            throw new BaseException(ErrorCode.DB_EXCEPTION, e);
        }
        finally
        {
            if (conn != null)
            {
                try
                {
                    conn.close();
                }
                catch (SQLException e)
                {
                    throw new BaseException(ErrorCode.CON_NOT_CLOSE, e);
                }
            }
        }
    }
    
    /**
     * 修改自荐项目信息的推荐状态，将指定的自荐项目信息的推荐状态设置为指定的状态，同时设置首页推荐和栏目推荐两个标识
     * @param selloneselfid:自荐项目的uuid
     * @param recommend:推荐装填，0：不推荐，1：推荐
     */
    public void selloneselfRecommend(String selloneselfid, String recommend)
    {
        Connection conn = null;
        try
        {
            conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
        	//将指定的技术需求信息审批状态设置为审批通过状态
        	DataBaseExecutor m_executor = DataBaseExecutor.getExecutor(conn);
        	String sql = MarketSql.updateSelloneselfRecommendSql(selloneselfid, recommend);
        	m_executor.execute(sql);
        }
        catch (SQLException e)
        {
            throw new BaseException(ErrorCode.DB_EXCEPTION, e);
        }
        finally
        {
            if (conn != null)
            {
                try
                {
                    conn.close();
                }
                catch (SQLException e)
                {
                    throw new BaseException(ErrorCode.CON_NOT_CLOSE, e);
                }
            }
        }
    }
    
    /**
     * 删除自荐项目信息
     * @param selloneselfid:自荐项目的uuid
     */
    public void selloneselfDelete(String selloneselfid)
    {
        Connection conn = null;
        try
        {
            conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
        	//将指定的技术需求信息审批状态设置为审批通过状态
        	DataBaseExecutor m_executor = DataBaseExecutor.getExecutor(conn);
        	String sql = MarketSql.deleteSelloneselfSql(selloneselfid);
        	m_executor.execute(sql);
        }
        catch (SQLException e)
        {
            throw new BaseException(ErrorCode.DB_EXCEPTION, e);
        }
        finally
        {
            if (conn != null)
            {
                try
                {
                    conn.close();
                }
                catch (SQLException e)
                {
                    throw new BaseException(ErrorCode.CON_NOT_CLOSE, e);
                }
            }
        }
    }
    
    /**
     * 获取用户提交的投融资项目信息
     */
    public RecordSet getInvestList(RequestKit requestKit, PagingInfo pagingInfo)
    {
        Connection conn = null;
        RecordSet recordSet = null;
        try
        {
            conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
            DataBaseExecutor m_executor = DataBaseExecutor.getExecutor(conn);
            String sql = MarketSql.getInvestListSql(requestKit);
            recordSet = m_executor.find(sql, pagingInfo);
            return recordSet;
        }
        catch (SQLException e)
        {
            throw new BaseException(ErrorCode.DB_EXCEPTION, e);
        }
        finally
        {
            if (conn != null)
            {
                try
                {
                    conn.close();
                }
                catch (SQLException e)
                {
                    throw new BaseException(ErrorCode.CON_NOT_CLOSE, e);
                }
            }
        }
    }
    
    /**
     * 根据uuid获取投融资项目的详细信息
     */
    public Record getInvestById(String uuid)
    {
        Connection conn = null;
        RecordSet recordSet = null;
        Record record = null;
        try
        {
            conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
            DataBaseExecutor m_executor = DataBaseExecutor.getExecutor(conn);
            String sql = MarketSql.getInvestInfoSql(uuid);
            recordSet = m_executor.find(sql);
            if(recordSet!=null && recordSet.size()>0){
            	record = recordSet.get(0);
            }
            return record;
        }
        catch (SQLException e)
        {
            throw new BaseException(ErrorCode.DB_EXCEPTION, e);
        }
        finally
        {
            if (conn != null)
            {
                try
                {
                    conn.close();
                }
                catch (SQLException e)
                {
                    throw new BaseException(ErrorCode.CON_NOT_CLOSE, e);
                }
            }
        }
    }
    
    /**
     * 修改投融资项目信息的审批状态，将指定的投融资项目信息的审批状态设置为指定的状态
     * @param investid:投融资项目的uuid
     * @param ifcheck:审核状态，1审核通过，2审核不通过
     */
    public void investCheckStatus(String investid, String ifcheck)
    {
        Connection conn = null;
        try
        {
            conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
        	
        	//将指定的技术需求信息审批状态设置为审批通过状态
        	DataBaseExecutor m_executor = DataBaseExecutor.getExecutor(conn);
        	String sql = MarketSql.updateInvestCheckSql(investid, ifcheck);
        	m_executor.execute(sql);
        }
        catch (SQLException e)
        {
            throw new BaseException(ErrorCode.DB_EXCEPTION, e);
        }
        finally
        {
            if (conn != null)
            {
                try
                {
                    conn.close();
                }
                catch (SQLException e)
                {
                    throw new BaseException(ErrorCode.CON_NOT_CLOSE, e);
                }
            }
        }
    }
    
    /**
     * 修改投融资项目信息的推荐状态，将指定的投融资项目信息的推荐状态设置为指定的状态，同时设置首页推荐和栏目推荐两个标识
     * @param investid:投融资项目的uuid
     * @param recommend:推荐装填，0：不推荐，1：推荐
     */
    public void investRecommend(String investid, String recommend)
    {
        Connection conn = null;
        try
        {
            conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
        	//将指定的技术需求信息审批状态设置为审批通过状态
        	DataBaseExecutor m_executor = DataBaseExecutor.getExecutor(conn);
        	String sql = MarketSql.updateInvestRecommendSql(investid, recommend);
        	m_executor.execute(sql);
        }
        catch (SQLException e)
        {
            throw new BaseException(ErrorCode.DB_EXCEPTION, e);
        }
        finally
        {
            if (conn != null)
            {
                try
                {
                    conn.close();
                }
                catch (SQLException e)
                {
                    throw new BaseException(ErrorCode.CON_NOT_CLOSE, e);
                }
            }
        }
    }
    /**
     * 删除投融资项目信息
     * @param investid:投融资项目项目的uuid
     */
    public void investDelete(String investid)
    {
        Connection conn = null;
        try
        {
            conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
        	//将指定的技术需求信息审批状态设置为审批通过状态
        	DataBaseExecutor m_executor = DataBaseExecutor.getExecutor(conn);
        	String sql = MarketSql.deleteInvestSql(investid);
        	m_executor.execute(sql);
        }
        catch (SQLException e)
        {
            throw new BaseException(ErrorCode.DB_EXCEPTION, e);
        }
        finally
        {
            if (conn != null)
            {
                try
                {
                    conn.close();
                }
                catch (SQLException e)
                {
                    throw new BaseException(ErrorCode.CON_NOT_CLOSE, e);
                }
            }
        }
    }
    
    /**
     * 从T_FieldOption获取字段选项
     */
    public RecordSet getFildOption(String tablename,String fieldname)
    {
        Connection conn = null;
        RecordSet recordSet = null;
        try
        {
            conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
            DataBaseExecutor m_executor = DataBaseExecutor.getExecutor(conn);
            String sql = UserNeedsSql.getFildOption(tablename,fieldname);
            recordSet = m_executor.find(sql);
            return recordSet;
        }
        catch (SQLException e)
        {
            throw new BaseException(ErrorCode.DB_EXCEPTION, e);
        }
        finally
        {
            if (conn != null)
            {
                try
                {
                    conn.close();
                }
                catch (SQLException e)
                {
                    throw new BaseException(ErrorCode.CON_NOT_CLOSE, e);
                }
            }
        }
    }
    
    public static String getCalculateDate(int iyear,int imonth,int iday) {
        Calendar c = Calendar.getInstance();
        //当前日期
        c.setTime(new Date(System.currentTimeMillis()));
        int day = c.get(Calendar.DATE);
        int month = c.get(Calendar.MONTH);
        int year = c.get(Calendar.YEAR);
        
        c.set(Calendar.YEAR, year+iyear);
        c.set(Calendar.MONTH, month+imonth);
        c.set(Calendar.DATE, day+iday);
        
        String returndate = DateUtility.getString(c.getTime(), "yyyy-MM-dd");
        //System.out.println(returndate);
        return returndate;
    }
    
}
